import 'package:flutter/material.dart';
import 'package:dome/http/api.dart';
import 'package:dome/tool/Icon.dart';
import 'package:dome/widget/widget_screen_button.dart';

// 选择支付方式
typedef Success<Map> = Function(Map params);
selectPatternOfPayment(BuildContext context, double price, {success, Map? params}) {
  print(params);
  List _way = [
    {
      'id': 1,
      'name': '支付宝',
      'img': 'lib/assets/images/zhifubao.png',
    },
    {
      'id': 2,
      'name': '支付宝',
      'img': 'lib/assets/images/weixin.png',
    }
  ];

  ValueNotifier<int> _groupValue = ValueNotifier<int>(1);
  showModalBottomSheet(
    context: context,
    isScrollControlled: false,
    backgroundColor: Colors.white,
    isDismissible: false,
    shape: RoundedRectangleBorder(borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10))),
    builder: (BuildContext context) {
      return Container(
        height: 320,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            DecoratedBox(
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(color: Color.fromRGBO(242, 242, 242, 1), width: 1, style: BorderStyle.solid),
                ),
              ),
              child: Padding(
                padding: EdgeInsets.all(10),
                child: Flex(
                  direction: Axis.horizontal,
                  children: [
                    GestureDetector(
                      onTap: () {
                        Navigator.pop(context);
                      },
                      child: IconDatas.iconhebingxingzhuang(color: Colors.grey[400]),
                    ),
                    Expanded(
                      flex: 1,
                      child: Center(
                        child: Text('收银台'),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(0, 20, 0, 10),
              child: Flex(
                direction: Axis.vertical,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text('¥'),
                      Text(
                        '$price',
                        style: TextStyle(fontSize: 20),
                      ),
                    ],
                  ),
                  Text('支付金额'),
                ],
              ),
            ),
            Column(
              children: List.generate(
                _way.length,
                (index) => ValueListenableBuilder(
                    valueListenable: _groupValue,
                    builder: (BuildContext context, int value, Widget? child) {
                      return RadioListTile<int>(
                        title: Flex(
                          direction: Axis.horizontal,
                          children: [
                            SizedBox(
                              width: 30,
                              height: 30,
                              child: ClipRRect(
                                borderRadius: BorderRadius.all(Radius.circular(5)),
                                child: Image.asset(_way[index]['img']),
                              ),
                            ),
                            Padding(
                              padding: EdgeInsets.only(left: 10),
                              child: Text(_way[index]['name']),
                            ),
                          ],
                        ),
                        controlAffinity: ListTileControlAffinity.trailing,
                        value: _way[index]['id'],
                        groupValue: _groupValue.value,
                        onChanged: (e) {
                          _groupValue.value = _way[index]['id'];
                        },
                      );
                    }),
              ),
            ),
            Padding(
              padding: EdgeInsets.all(20),
              child: WidgetScreenButton(
                color: Colors.red,
                text: '确认支付',
                onTap: () async {
                  Api.zhifubao(
                    pathParams: {
                      'money': params!['amount'],
                      'nextAction': {'type': 0, 'id': params['orderNumber']}
                    },
                    successCallBack: (res) async {
                      // print(await FlutterAlipay.isInstalled());
                      // if (await FlutterAlipay.isInstalled()) {
                      // aliPay(convert.jsonEncode(res['data']));
                      // EasyLoading.showToast('已经安装');
                      // } else {
                      //   EasyLoading.showToast('没有安装');
                      // }
                    },
                  );
                },
              ),
            ),
          ],
        ),
      );
    },
  );
}
